﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Weber.CS3280.Interfaces;
using Weber.CS3280.UI.ViewModels;

namespace Weber.CS3280.Inventory.UI.Windows
{
	/// <summary>
	/// Interaction logic for SingleControlHostWindow.xaml
	/// </summary>
	public partial class SingleControlHostWindow : Window, IModalWindow
	{
		private bool _isModal;

		public SingleControlHostWindow()
		{
			InitializeComponent();
		}

		protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
		{
			ICustomCleanup cleanup = DataContext as ICustomCleanup;

			if (cleanup != null)
				cleanup.DoCleanup();

			base.OnClosing(e);
		}

		/// <summary>
		/// Displays a Modeless Window containing the control (of T)
		/// </summary>
		/// <typeparam name="T">Type of Control that will be instantiated and displayed</typeparam>
		/// <param name="viewModel"></param>
		/// <param name="owner"></param>
		public static void ShowWindow<T>(ViewModelBase viewModel, Window owner) where T : Control, new()
		{
			SingleControlHostWindow window = new SingleControlHostWindow();
			// Create the hosted control
			T hostedControl = new T();

			// Assign the control into the window
			window.Content = hostedControl;
			// Assign the view model to the data context
			window.DataContext = viewModel;
			window.Owner = owner;
			window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
			window.Show();
		}

		public static bool ShowModal<T>(ViewModelBase viewModel, Window owner) where T : Control, new()
		{
			SingleControlHostWindow window = new SingleControlHostWindow();
			// Create the hosted control
			T hostedControl = new T();

			// Assign the control into the window
			window.Content = hostedControl;
			// Assign the view model to the data context
			window.DataContext = viewModel;
			window.Owner = owner;
			window.WindowStartupLocation = WindowStartupLocation.CenterOwner;
			window._isModal = true;

			// Check for valid result from the Dialog
			return window.ShowDialog() ?? false;
		}

		public bool IsModal
		{
			get { return _isModal; }
		}
	}
}
